#pragma once

#include "libs/easylog.h"
#include "util.hpp"

namespace pharmacy_server
{
    class config
    {
    public:
        static config& instance()
        {
            static config cfg;
            return cfg;
        }

        uint16_t server_port() { return _server_port; }
        const std::string& sql_user() { return _sql_user; }
        const std::string& sql_passwd() { return _sql_passwd; }
        const std::string& sql_host() { return _sql_host; }
        uint16_t sql_port() { return _sql_port; }
        const std::string& sql_dbname() { return _sql_dbname; }

    private:
        config()
        {
            easyfs::file conf("resources/config.json");

            if (!conf.exists())
                throw except("config.json is not found");

            Json::Value root = json_util::deserialize(conf.read());

            _server_port = root["server_port"].asUInt();
            _sql_user = root["sql_user"].asString();
            _sql_passwd = root["sql_passwd"].asString();
            _sql_host = root["sql_host"].asString();
            _sql_port = root["sql_port"].asUInt();
            _sql_dbname = root["sql_dbname"].asString();

            easylog::info("config instance created");

            easylog::debug("\n\tserver_port: {}\n\tsql_user: {}\n\tsql_passwd: {}\n\tsql_host: {}\n\tsql_port: {}\n\tsql_dbname: {}",
                _server_port, _sql_user, _sql_passwd, _sql_host, _sql_port, _sql_dbname);
        }

        config(const config&) = delete;
        config& operator=(const config&) = delete;

    private:
        uint16_t    _server_port;
        std::string _sql_user;
        std::string _sql_passwd;
        std::string _sql_host;
        uint16_t    _sql_port;
        std::string _sql_dbname;
    };
}
